package com.relytosoft.mizar.media.alarm.services.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.relytosoft.dubhe.common.core.base.page.PageQuery;
import com.relytosoft.dubhe.common.core.object.ResponseResult;
import com.relytosoft.dubhe.common.core.utils.BeanCopyUtils;
import com.relytosoft.dubhe.common.core.utils.CommonUtils;
import com.relytosoft.dubhe.common.log.annotation.OpLog;
import com.relytosoft.dubhe.common.log.enums.OpTypeEnum;
import com.relytosoft.mizar.media.alarm.api.pojo.dto.AyControlAlarmDto;
import com.relytosoft.mizar.media.alarm.api.pojo.query.AlarmAnalyzeQuery;
import com.relytosoft.mizar.media.alarm.api.pojo.query.AlarmControlQuery;
import com.relytosoft.mizar.media.alarm.api.pojo.query.AlarmQuery;
import com.relytosoft.mizar.media.alarm.api.pojo.vo.AlarmListVo;
import com.relytosoft.mizar.media.alarm.api.pojo.vo.AlarmVo;
import com.relytosoft.mizar.media.alarm.services.service.AlarmService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author wss
 * @version 1.0
 * @date 2022/5/24 14:16
 */
@Api("告警信息查询")
@RestController
@RequestMapping("alarm")
public class AlarmController {

    @Autowired
    private AlarmService alarmService;

    @PostMapping("receive")
    public ResponseResult accept(@RequestBody String alarmStr) {
        AyControlAlarmDto ayControlAlarmDto = JSON.parseObject(alarmStr, AyControlAlarmDto.class);
        if (alarmService.alarmAccept(ayControlAlarmDto)) {
            return ResponseResult.ok();
        }
        return ResponseResult.fail();
    }


    @OpLog(msg = "布控告警信息查询", opType = OpTypeEnum.SELECT)
    @ApiOperation("布控告警信息查询")
    @GetMapping("/controlalarmlist")
    public ResponseResult<Map<String, Object>> controlAlarmList(AlarmControlQuery alarmControlQuery, PageQuery pageQuery) {
        AlarmQuery alarmQuery= BeanCopyUtils.copy(alarmControlQuery,AlarmQuery.class);
        alarmQuery.setAlarmTypeCategory(1000);
        IPage<AlarmListVo> page = alarmService.selectPageAlarmList(pageQuery, alarmQuery);
        return ResponseResult.ok(CommonUtils.getPageData(page));
    }

    @ApiOperation("布控任务选择列表")
    @GetMapping({"/controltasklist"})
    public ResponseResult controlTaskList(@RequestParam(required = false) Integer type) {
        return ResponseResult.ok(alarmService.selectControlTaskList(type));
    }

    @OpLog(msg = "分析告警信息查询", opType = OpTypeEnum.SELECT)
    @ApiOperation("分析告警信息查询")
    @GetMapping("/analyzealarmlist")
    public ResponseResult<Map<String, Object>> analyzeAlarmList(AlarmAnalyzeQuery alarmAnalyzeQuery, PageQuery pageQuery) {
        AlarmQuery alarmQuery= BeanCopyUtils.copy(alarmAnalyzeQuery,AlarmQuery.class);
        alarmQuery.setAlarmTypeCategory(1001);
        IPage<AlarmListVo> page = alarmService.selectPageAlarmList(pageQuery, alarmQuery);
        return ResponseResult.ok(CommonUtils.getPageData(page));
    }

    @ApiOperation("查询告警信息详情")
    @GetMapping("/alarmdetail/{id}")
    public ResponseResult<AlarmVo> alarmDetail(@PathVariable Long id) {
        return ResponseResult.ok(alarmService.selectAlarmDetail(id));
    }

    /**
     * @param alarmCategory 1000布控类别 1001 分析类别
     * @return
     */
    @ApiOperation("告警类型选择列表")
    @GetMapping({"/alarmTypelist"})
    public ResponseResult alarmTypelist(@RequestParam(required = false) Integer alarmCategory) {
        return ResponseResult.ok(alarmService.selectAlarmTypeList(alarmCategory));
    }
}
